package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.LookAction;
import gatperdut.chato.core.interpreter.grammar.comminfo.LookInfo;
import gatperdut.chato.core.item.ItemController;

public class LookInAction extends LookAction {

    public LookInAction( LookInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        StringBuilder message = new StringBuilder( "" );

        ItemController ic = null;
        String where = null;
        if ( !getInfo().isGround() ) {
            ic = getEntityController().getInventoryController().getItemController( getInfo().getWhat() );
            where = "carried";
        }
        if ( getInfo().isGround() || ( !getInfo().isGround() && ic == null ) ) {
            ic = getEntityController().getRoomController().getInventoryController().getItemController( getInfo().getWhat() );
            where = "in room";
        }
        if ( ic != null ) {
            if ( ic.getInventoryController() == null ) {
                getEntityController().tx( ic.getShortDesc() + " is not a container." );
                return CommandExecutionResult.OK;
            }
            if ( !ic.getItem().getInventory().isOpen() ) {
                getEntityController().tx( ic.getShortDesc() + " is closed." );
                return CommandExecutionResult.OK;
            }
            String contents = ic.getInventoryController().listContentsShortDescription( true );
            if ( "".equals( contents ) ) {
                message.append( ic.getShortDesc() + " (" + where + ") is empty." );
            }
            else {
                message.append( ic.getShortDesc() + " (" + where + ") contains:" + CSConstantsString.NEWLINE + contents );
            }
            getEntityController().tx( message.toString() );
            return CommandExecutionResult.OK;
        }
        else {
            getEntityController().tx( "You cannot find that here." );
            return CommandExecutionResult.OK;
        }
    }
}
